
const { BettingProcessMgr, BettingType } = require("./bettingProcessMgr");
const { SIGNAL_DEFINE } = require("./signalDefines");

const DXOptionalOperate = {
    EAT_TOUCH_BELT: 0,
    EAT_TOUCH: 1,
    BELT: 2,
    EAT: 3,
};

const DXOperate = {
    EAT: 0,     // 吃
    TOUCH: 1,   // 碰
    BELT: 2,    // 带
    WAIVE: 3,   // 弃
};

class DXBettingProcessMgr extends BettingProcessMgr {
    constructor() {
        super();
        this.fundPool = 0;
        this.beltNowCost = 0;
        this.gameMgr = null;
        this.isOnlyEat = false;
    }

    setGameMgr(gameMgr) {
        super.setGameMgr(gameMgr);
        this.banker = gameMgr.getBanker();
        this.beltNowCost = gameMgr.getBaseScore();
        this.fundPool = gameMgr.getFundPool();
        this.isOnlyEat = gameMgr.isOnlyEat;
    }

    isOperateLegal(seatIndex, value) {
        var op = getOpByValue(value);
        if (op == null) {
            return false;
        }
        var optionalOp = this.getOptionalOperates(seatIndex);
        if (op == DXOperate.EAT) {
            if (optionalOp != DXOptionalOperate.BELT) {
                return true;
            }
            return false;
        } else if (op == DXOperate.TOUCH) {
            if (optionalOp == DXOptionalOperate.EAT_TOUCH || optionalOp == DXOptionalOperate.EAT_TOUCH_BELT) {
                return true;
            }
            return false;
        } else if (op == DXOperate.BELT) {
            if (optionalOp == DXOptionalOperate.BELT || optionalOp == DXOptionalOperate.EAT_TOUCH_BELT) {
                return true;
            }
            return false;
        } else {
            return true;
        }
    }

    turnGamberBetting(seatIndex) {
        super.turnGamberBetting(seatIndex);
        var userId = this.seatMgr.getUserIdBySeatIndex(this.turnGamber);
        var operates = this.getOptionalOperates(seatIndex);
        var extraData = {
            fundPool: this.fundPool,
            beltCost: this.beltNowCost,
        }
        this.signal.emit(SIGNAL_DEFINE.BETTING_TURN, userId, operates, extraData);
    }

    getOpByValue(value) {
        if (value == this.beltNowCost) {
            return DXOperate.BELT;
        } else if (value == this.fundPool) {
            return DXOperate.EAT;
        } else if (value == 0) {
            return DXOperate.WAIVE;
        } else if (value >= this.gameMgr.baseScore && value < this.fundPool) {
            return DXOperate.TOUCH;
        }
    }

    getOptionalOperates(seatIndex) {
        if (this.isOnlyEat) {   // 头家闷吃
            if (this.banker == seatIndex) {
                if (this.turnRound == 1) {
                    return DXOptionalOperate.EAT;
                } else {    // 头家反带
                    return DXOptionalOperate.BELT;
                }
            } else {
                return DXOptionalOperate.BELT;
            }
        } else {
            if (this.banker == seatIndex) {
                return DXOptionalOperate.EAT_TOUCH;
            } else {
                return DXOptionalOperate.EAT_TOUCH_BELT;
            }
        }
    }

    betting(seatIndex, value) {
        super.betting(seatIndex, value);
        this.beltNowCost += value;
        
        var userId = this.seatMgr.getUserIdBySeatIndex(seatIndex);
        this.signal.emit(SIGNAL_DEFINE.BETTING, userId, value);
    }

}

exports.BettingType = BettingType;
exports.DXBettingProcessMgr = DXBettingProcessMgr;